import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import {FetchService} from "../../../../../core/net/fetch.service";
import * as _ from 'lodash';
import { DispatchPlanService } from '../../../dispatch-plan.service';
import { NzMessageService } from 'ng-zorro-antd';

const glcMockData = [
  {name: '唐徕渠管理处', status: 1},
  {name: '西干渠管理处', status: 1},
  {name: '惠农渠管理处', status: 1},
  {name: '汉延渠管理处', status: 1},
  {name: '秦汉渠管理处', status: 2},
  {name: '唐徕渠管理处', status: 2},
  {name: '唐徕渠管理处', status: 2},
  {name: '唐徕渠管理处', status: 2},
  {name: '唐徕渠管理处', status: 2},
  {name: '唐徕渠管理处', status: 2},
  {name: '唐徕渠管理处', status: 2},
];
const reportTypeMap = {
  1: '未报送',
  2: '已报送'
};

@Component({
  selector: 'app-slt-dispatch-plan-overview',
  templateUrl: './slt-dispatch-plan-overview.component.html',
  styleUrls: ['./slt-dispatch-plan-overview.component.scss']
})
export class SltDispatchPlanOverviewComponent implements OnInit {
  summaryType = 'year';   //  year-年概要 month-月旬概要
  glcFinishedNum = 0;
  glcUnfinishedNum = 0;
  glcPercentage = "";
  glcData = glcMockData;
  glcTableData1: Array<any>;
  glcTableData2: Array<any>;

  cityFinishedNum = 0;
  cityUnfinishedNum = 0;
  cityPercentage: number | null = null;
  cityVal = 0;
  cityData = [];
  companyFinishedNum = 0;
  companyUnfinishedNum = 0;
  companyPercentage: number | null = null;
  companyData = [];
  glcMonthData = [];
  constructor(
    private router: Router,
    private dispatchService: DispatchPlanService,
    private routerInfo: ActivatedRoute,
    private message: NzMessageService,
    private fetchService: FetchService) { }

  ngOnInit() {
      this.getData();
  }
  formatPercentage = (percent: number) => `报送进度 ${percent}%`;

  getData() {
    this.getMngData();
  }

  getMngData() {
    const cache = Object.assign({}, this.dispatchService.getNextDateInterval());
    const params = {
      "apicode": "18",
      "cycle": cache.y,
      "m": cache.m,
      "dataType":	cache.dataType || null,
    };
    this.fetchService.post<any>('sys/sysApiInfo/queryApiData', params).subscribe(res => {
      if (Array.isArray(res)) {
          this.glcTableData1 = res.slice(0, 6);
          this.glcTableData2 = res.slice(6, 12);
          console.log(this.glcTableData1, this.glcTableData2, res);
          res.forEach(item => {
            if (item.state === '1') {
              ++this.glcFinishedNum;
            } else {
              ++this.glcUnfinishedNum;
            }
          });
          this.glcPercentage = (this.glcFinishedNum / 12 * 100).toFixed(2);
      } else {

      }
  });
  }

    getSummaryData(): void {
        this.glcData = glcMockData.map(item => {
            return _.assign(_.cloneDeep(item), {statusName: reportTypeMap[item.status]});
        });
    }

    getCityReportDetailData(): void {

    }

    onEdit(data, isEditable: boolean = false): void {
        this.router.navigate(['/dispatch-plan/slt-dispatch-detail'], { skipLocationChange: true, queryParams: {isEditable: isEditable, id: data.id}});

    }
    exportMonthFile(data): void {

    }
    // 一键报送
    sendToMng(): void {
      const cache = Object.assign({}, this.dispatchService.getNextDateInterval());
      const params = {
        "apicode": "18",
        "y": cache.y,
        "m": cache.m,
        "dataType":	cache.dataType || null,
      };
      this.fetchService.post<any>('plan/wrsReportData/waterPlanInform', params).subscribe(res => {
        this.message.success(`催报成功`);
    });
    }
}
